package com.jdb.loan.controller.assembler;

import com.alibaba.fastjson.JSON;
import com.jdb.loan.api.dto.LoanDTO;
import com.jdb.loan.provider.entity.LoanDO;
import org.springframework.cglib.beans.BeanCopier;

import java.util.Objects;

public class LoanAssembler {
    private static final BeanCopier fromCopier = BeanCopier.create(
            LoanDTO.class, LoanDO.class, false);
    private static final BeanCopier toCopier = BeanCopier.create(
            LoanDO.class, LoanDTO.class, false);

    public static LoanDO fromDTO(LoanDTO loanDTO) {
        if (Objects.isNull(loanDTO)) {
            return null;
        }
        LoanDO loanDO = new LoanDO();
        fromCopier.copy(loanDTO, loanDO, null);
        loanDO.setTags(JSON.toJSONString(loanDTO.getTags()));
        return loanDO;
    }

    public static LoanDTO toDTO(LoanDO loanDO) {
        if (Objects.isNull(loanDO)) {
            return null;
        }
        LoanDTO loanDTO = new LoanDTO();
        toCopier.copy(loanDO, loanDTO, null);
        loanDTO.setTags(JSON.parseObject(loanDO.getTags()));
        return loanDTO;
    }
}
